// 前端配置文件

// 默认API地址
export const DEFAULT_API_BASE_URL = "https://a.gxc1994.top:3002";

// 自动检测当前域名和端口
const getCurrentOrigin = () => {
  if (typeof window !== "undefined") {
    return window.location.origin;
  }
  return DEFAULT_API_BASE_URL;
};

const currentOrigin = getCurrentOrigin();

export const config = {
  // 开发环境配置
  development: {
    apiBaseUrl: DEFAULT_API_BASE_URL,
    uploadUrl: `${DEFAULT_API_BASE_URL}/upload`,
    convertUrl: `${DEFAULT_API_BASE_URL}/convert`,
    downloadUrl: `${DEFAULT_API_BASE_URL}/download`,
  },

  // 生产环境配置 - 使用当前域名
  production: {
    apiBaseUrl: DEFAULT_API_BASE_URL,
    uploadUrl: `${DEFAULT_API_BASE_URL}/upload`,
    convertUrl: `${DEFAULT_API_BASE_URL}/convert`,
    downloadUrl: `${DEFAULT_API_BASE_URL}/download`,
  },
};

const env = import.meta.env.MODE || "development";
const defaultConfig = config[env] || config.development;

// 同步获取API地址（优先localStorage/chrome.storage）
export function getApiBaseUrlSync() {
  if (typeof window !== "undefined") {
    if (
      typeof chrome !== "undefined" &&
      chrome.storage &&
      chrome.storage.local &&
      chrome.runtime &&
      chrome.runtime.id
    ) {
      // chrome.storage.local 只能异步，无法同步获取
      // fallback: 先用localStorage
      return localStorage.getItem("apiBaseUrl") || defaultConfig.apiBaseUrl;
    }
    return localStorage.getItem("apiBaseUrl") || defaultConfig.apiBaseUrl;
  }
  return defaultConfig.apiBaseUrl;
}

// 异步获取API地址
export async function getApiBaseUrl() {
  if (
    typeof chrome !== "undefined" &&
    chrome.storage &&
    chrome.storage.local &&
    chrome.runtime &&
    chrome.runtime.id
  ) {
    return new Promise((resolve) => {
      chrome.storage.local.get(["apiBaseUrl"], (result) => {
        resolve(
          result.apiBaseUrl ||
            localStorage.getItem("apiBaseUrl") ||
            defaultConfig.apiBaseUrl
        );
      });
    });
  }
  return getApiBaseUrlSync();
}

export async function setApiBaseUrl(url) {
  if (
    typeof chrome !== "undefined" &&
    chrome.storage &&
    chrome.storage.local &&
    chrome.runtime &&
    chrome.runtime.id
  ) {
    chrome.storage.local.set({ apiBaseUrl: url });
  }
  localStorage.setItem("apiBaseUrl", url);
}

// 支持同步和异步的buildApiUrl
export function buildApiUrlSync(endpoint) {
  const base = getApiBaseUrlSync();
  return `${base}${endpoint}`;
}

export async function buildApiUrl(endpoint) {
  const base = await getApiBaseUrl();
  return `${base}${endpoint}`;
}

export function getUploadUrlSync() {
  const base = getApiBaseUrlSync();
  return `${base}/api/convert-to-webp`;
}

export async function getUploadUrl() {
  const base = await getApiBaseUrl();
  return `${base}/api/convert-to-webp`;
}

export function getConvertUrlSync() {
  const base = getApiBaseUrlSync();
  return `${base}/convert`;
}

export async function getConvertUrl() {
  const base = await getApiBaseUrl();
  return `${base}/convert`;
}

export function getDownloadUrlSync() {
  const base = getApiBaseUrlSync();
  return `${base}/download`;
}

export async function getDownloadUrl() {
  const base = await getApiBaseUrl();
  return `${base}/download`;
}
